//供应商
function formatterSupplier(value,row,index) {
    return value ? value.name :"";
}
//商品名称
function formatterProductName(value,row,index) {
    return row.product?row.product.productName:"";
}
//商品编码
function formatterProductSn(value,row,index) {
    return row.product ? row.product.sn :"";
}



$(function () {

    var returnGoods_datagrid = $("#returnGoods_datagrid");
    var returnGoods_dialog = $("#returnGoods_dialog");
    var returnGoods_dialog_datagrid = $("#returnGoods_dialog_datagrid");
    var SelectReturn_datagrid = $("#SelectReturn_datagrid");
    var return_dialog = $("#return_dialog");
    var return_dialog_form = $("#return_dialog_form")
    var dialog_sn = $("#dialog_sn")
    var dialog_product = $("#dialog_product")
    var product_datagrid = $("#product_datagrid")


    var snData = null;
    //选择编码
    $("#dialog_sn").combobox({
        onChange:function (newValue,oldValue) {
            var Datas = snData[newValue-1]
            var coData = snData[0].bills;
            console.log(snData[0].bills);
            $("#dialog_product").combobox("loadData",{
                valueField:"id",
                textField:coData.id
            });
            $("#return_dialog_form").form("load",coData);
        },
        onLoadSuccess:function (data) {
            snData = $.extend(true,{},data);
            console.log(snData);
        }
    });
    //商品列表
    product_datagrid.datagrid({
        url:"/stockincome/getBillList",
        fit:true,
        fitColumns:true,
        rownumbers:true,
        singleSelect:true,
        border:false,
        columns:[[
            {field:"supplier",title:"供应商",width:100,formatter:formatterSupplier},
            {field:"product",title:"商品名称",width:100,formatter:formatterProductName},
            {field:"costPrice",title:"单价",width:100},
            {field:"number",title:"数量",width:100},
            {field:"amountPrice",title:"合计",width:100},
            {field:"billId",title:"billId",width:100,hidden:true},
        ]],
        onLoadSuccess:function (data) {
            console.log(data);
        }
    });

    //列表
    returnGoods_datagrid.datagrid({
        url:"/returnGoods/list",
        fit:true,
        fitColumns:true,
        rownumbers:true,
        singleSelect:true,
        border:false,
        columns:[[
            {field:"sn",title:"单据编号",width:100},
            {field:"supplier",title:"供应商",width:100,formatter:formatterSupplier},
            {field:"productName",title:"商品名称",width:100,formatter:formatterProductName},
            {field:"productId",title:"商品编码",width:100,formatter:formatterProductSn},
            {field:"costPrice",title:"单价",width:100},
            {field:"number",title:"数量",width:100},
            {field:"amountPrice",title:"合计",width:100},
            {field:"inputTime",title:"操作时间",width:100},
            {field:"operator",title:"操作人员",width:100}
        ]],
        toolbar:"#linkbutton",
        onLoadSuccess:function (data) {
            console.log(data);
        }
    })

    dialog_sn.datagrid({
        onChange:function (newValue,oldValue) {
            console.log(newValue);
        },
        onLoadSuccess:function (data) {
            console.log(data);
            dialog_product.combobox("loadData",{
                // valueField:data.
            });
        }
    })


    cmdObj = {
        //新增退货
        returnGoodsAdd:function () {
            returnGoods_dialog.dialog("open")
        },
        add:function () {
            return_dialog.dialog("open")
        },
        return:function () {
            var selectedRow = $(product_datagrid).datagrid("getSelected");
            if (!selectedRow){
                $.messager.alert("温馨提醒","请先选中要退货的行","error");
                return;
            }
            console.log(selectedRow);
            var selectData=JSON.stringify(selectedRow)
            $.post("/returnGoods/returnGoods",{
                // id:selectedRow.id,
                // product :selectedRow.product,
                // supplier:selectedRow.supplier,
                // billId:selectedRow.billId,
                // number:selectedRow.number,
                // unit:selectedRow.unit,
                // costPrice:selectedRow.costPrice,
                // amountPrice:selectedRow.amountPrice
                id:selectedRow.id
            },function (data) {
                    if(data.success){
                        $.messager.alert("温馨提醒","操作成功","info",function () {
                            returnGoods_datagrid.datagrid("reload");
                            return_dialog.dialog("close");
                        })
                    }else{
                        $.messager.alert("温馨提醒",data.message,"error");
                    }
            });
            // return_dialog_form.form("submit",{
            //     url:"/returnGoods/returnGoods",
            //     success:function (data) {
            //         data = $.parseJSON(data);
            //         console.log(data)
            //         if(data.success){
            //             $.messager.alert("温馨提醒","操作成功","info",function () {
            //                 returnGoods_datagrid.datagrid("reload");
            //                 return_dialog.dialog("close");
            //             })
            //         }else{
            //             $.messager.alert("温馨提醒",data.message,"error");
            //         }
            //     }
            // })
        }

    }

    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        console.log(cmd);
        cmdObj[cmd]();
    })

})

